"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : ALTER PROCEDURE
Case Name   : 非系统管理员的非所有者修改procedure所属模式为public,合理报错
Create At   : 2022/05/10
Owner       : @zou_jialiang050
Description :
    1.创建用户1
    2.创建用户2
    3.用户1创建procedure
    4.对用户2赋予用户1模式的所有权限
    5.用户2修改procedure的所属模式为public
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.合理报错
    6.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class PROCEDURE(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user1 = 'u_alter_procedure_0016_01'
        self.user2 = 'u_alter_procedure_0016_02'
        self.procedure = 'p_alter_procedure_0016'
        self.schema = 's_alter_procedure_0016'

    def test_alter_procedure(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:创建用户1 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user1} cascade;
            create user {self.user1} with password '{macro.PASSWD_REPLACE}';
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:创建用户2 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user2} cascade;
            create user {self.user2} with password '{macro.PASSWD_REPLACE}';
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step3:用户1创建procedure expect:成功'
        self.logger.info(step)
        create_pro = self.primary_sh.execut_db_sql(
            f'''create or replace procedure {self.procedure}()
            is
            var1 int:= 0;
            begin
                var1 := (select 1+1);
            end;''', sql_type=f" -U {self.user1} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(create_pro)
        self.assertIn(self.Con.CREATE_PROCEDURE_SUCCESS_MSG, create_pro,
                      '执行失败' + step)

        step = 'step4:对用户2赋予用户1模式的所有权限 expect:成功'
        self.logger.info(step)
        grant_pri = self.primary_sh.execut_db_sql(
            f'''grant all privileges on schema {self.user1} to {self.user2};
            ''')
        self.logger.info(grant_pri)
        self.assertIn('GRANT', grant_pri, '执行失败' + step)

        step = 'step5:用户2修改procedure的所属模式为public expect:合理报错'
        self.logger.info(step)
        alter_procedure = self.primary_sh.execut_db_sql(
            f'''alter procedure {self.user1}.{self.procedure}() set schema 
            public;''', sql_type=f" -U {self.user2} -W "
            f"'{macro.PASSWD_REPLACE}'")
        self.logger.info(alter_procedure)
        self.assertIn(f'ERROR:  permission denied for schema public',
                      alter_procedure, '执行失败' + step)

    def tearDown(self):
        step = 'step6:清理环境 expect:清理环境成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop user {self.user1} cascade;
            drop user {self.user2};''')
        self.logger.info(clean_environment)
        self.assertEqual(clean_environment.count
                         (self.Con.DROP_ROLE_SUCCESS_MSG), 2,
                         '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
